package org.cabbage.lintcode;

/**
 * 给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符。
 *
 * @author gezhangyuan
 */
public class Plus {
    public static void main(String[] args) {
        System.out.println(aplusb(1, 2));
    }

    public static int aplusb(int a, int b) {
        int sum = 0;
        int carry = 0;
        for (int i = 0; i < 32; i++) {
            int a1 = a & 1;
            int b1 = b & 1;
            int val = 0;
            if (a1 == 0 && b1 == 0 && carry == 0) {
                val = 0;
                carry = 0;
            } else if (a1 == 1 && b1 == 1 && carry == 1) {
                val = 1;
                carry = 1;
            } else if (a1 == 0 && b1 == 0 || a1 == 0 && carry == 0 || b1 == 0 && carry == 0) {
                val = 1;
                carry = 0;
            } else {
                val = 0;
                carry = 1;
            }
            val = val << i;
            sum = sum | val;
            a = a >> 1;
            b = b >> 1;
        }
        return sum;
    }

}
